package com.jic.product.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.controller.rest.request.AddProductSearchHistoryRequest;
import com.jic.product.controller.rest.request.QueryProductNameRequest;
import com.jic.product.controller.rest.request.QueryProductSearchHistoryRequest;
import com.jic.product.controller.rest.vo.QueryProductNameVo;
import com.jic.product.controller.rest.vo.QueryProductSearchHistoryListVo;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.model.entity.MProductSearchHistory;
import com.jic.product.service.MProductSearchHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-08-07 15:04:47
 */
@Api(value = "API-历史搜索", tags = "API-历史搜索")
@RestController
@RequestMapping("/productSearchHistoryRest")
@Slf4j
public class ApiProductSearchHistoryRest {
	@Autowired
	private MProductSearchHistoryService service;
	@Resource
	private RedisClientTemplate redisClientTemplate;

	@ApiOperation(value = "新增历史搜索", notes = "新增历史搜索")
	@PostMapping("/saveMProductSearchHistory")
	public RestResult<Long> saveMProductSearchHistory(@RequestBody AddProductSearchHistoryRequest request) throws Throwable {
		MProductSearchHistory mProductSearchHistory = new MProductSearchHistory();
		BeanUtils.copyProperties(request, mProductSearchHistory);
		String token = request.getToken();
		String memStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + token);
		if (StringUtils.isEmpty(memStr)) {
			throw new AppRuntimeException(ResponseCode.NOT_LOGGED_IN);
		}
		Map memMap = JSONArray.parseObject(memStr, Map.class);
		Long userId = Long.valueOf(memMap.get("id").toString());
		mProductSearchHistory.setUserId(userId);
		mProductSearchHistory.setCreateTime(new Date());
		mProductSearchHistory.setDeleteFlag(0);
		long flag = service.save(mProductSearchHistory);
		return RestResult.success(flag);
	}

	@ApiOperation(value = "查询历史搜索", notes = "查询历史搜索")
	@PostMapping("/queryMProductSearchHistoryList")
	public RestResult<List<QueryProductSearchHistoryListVo>> queryMProductSearchHistoryList(@RequestBody QueryProductSearchHistoryRequest request) throws Throwable {
		MProductSearchHistory mProductSearchHistory = new MProductSearchHistory();
		String token = request.getToken();
		String memStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + token);
		if (StringUtils.isEmpty(memStr)) {
			throw new AppRuntimeException(ResponseCode.NOT_LOGGED_IN);
		}
		Map memMap = JSONArray.parseObject(memStr, Map.class);
		Long userId = Long.valueOf(memMap.get("id").toString());
		mProductSearchHistory.setUserId(userId);
		List<MProductSearchHistory> list = service.queryMProductSearchHistoryList(mProductSearchHistory);
		List<QueryProductSearchHistoryListVo> historList = new ArrayList<>();
		list.forEach(mProductSearchHistory1->{
			QueryProductSearchHistoryListVo vo = new QueryProductSearchHistoryListVo();
			vo.setCreateTime(mProductSearchHistory1.getCreateTime());
			vo.setWord(mProductSearchHistory1.getWord());
			historList.add(vo);
		});
		return RestResult.success(historList);
	}

	@ApiOperation(value = "删除历史搜索", notes = "删除历史搜索")
	@PostMapping("/deleteMProductSearchHistory")
	public RestResult<Long> deleteMProductSearchHistory(@RequestBody QueryProductSearchHistoryRequest request) throws Throwable {
		MProductSearchHistory mProductSearchHistory = new MProductSearchHistory();
		String token = request.getToken();
		String memStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + token);
		if (StringUtils.isEmpty(memStr)) {
			throw new AppRuntimeException(ResponseCode.NOT_LOGGED_IN);
		}
		Map memMap = JSONArray.parseObject(memStr, Map.class);
		Long userId = Long.valueOf(memMap.get("id").toString());
		mProductSearchHistory.setUserId(userId);
		long flag = service.updateByUserIdSelective(mProductSearchHistory);
		return RestResult.success(flag);
	}

	@ApiOperation(value = "商品名称搜索", notes = "商品名称搜索")
	@PostMapping("/queryProductNameBySearchHistory")
	public RestResult<List<QueryProductNameVo>> queryProductNameBySearchHistory(@RequestBody QueryProductNameRequest request) throws Throwable {
		MProductSearchHistory mProductSearchHistory = new MProductSearchHistory();
		BeanUtils.copyProperties(request, mProductSearchHistory);
		List<QueryProductNameVo>  list = service.queryProductNameBySearchHistory(mProductSearchHistory);
		return RestResult.success(list);
	}

}

